﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Xml;
using System.Xml.Serialization;
using ConvertSourceToHtml.Properties;
using ConvertSourceToHtml.Conversion.HtmlCodeElements;
using System.IO;
using System.Windows;
using System.Globalization;

namespace ConvertSourceToHtml.Troubleshooting
{

    /// <summary>
    /// Represents a scenario in which CopySourceAsHtml is used.
    /// </summary>
    /// <remarks>
    /// <para>
    /// <see cref="Scenario"/> is used for user bug reporting and for regression testing.
    /// </para>
    /// </remarks>
    public class Scenario
    {
        #region Properties

        /// <summary>
        /// Gets or sets the description of the scenario.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the name of the user who contributed the scenario, if any.
        /// </summary>
        public string ContributedBy { get; set; }

		/// <summary>
		/// Gets or sets the path of the file that was copied in this scenario.
		/// </summary>
		public string Path { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="Settings"/> to use when copying.
        /// </summary>
        public Settings Settings { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="ColorableItemStyle"/> to use to format plain text.
        /// </summary>
		[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "PlainText")]
		public ColorableItemStyle PlainTextStyle { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="ColorableItemStyle"/> to use to format line numbers.
        /// </summary>
        public ColorableItemStyle LineNumberStyle { get; set; }

        /// <summary>
        /// Gets or sets the RTF to copy as an XML CDATA section,
        /// </summary>
        /// <remarks>
        /// <para>
        /// This property should only be used for XML serialization.
        /// </para>
        /// <para>
        /// This property converts LF line endings to CRLF line endings when deserializing.
        /// </para>
        /// </remarks>
		[SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", MessageId = "System.Xml.XmlNode", Justification = "We want to expose XmlCDataSection.")]
		[XmlElement("Rtf")]
        public XmlCDataSection RtfSection
        {
            get { return new XmlDocument().CreateCDataSection(Rtf); }
            set { Rtf = value.Value.Replace("\n", "\r\n"); }
        }

        /// <summary>
        /// Gets or sets the HTML that should result from the copy as an XML CDATA section.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This property should only be used for XML serialization.
        /// </para>
        /// <para>
        /// This property converts LF line endings to CRLF line endings when deserializing.
        /// </para>
        /// </remarks>
		[SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", MessageId = "System.Xml.XmlNode", Justification = "We want to expose XmlCDataSection.")]
		[XmlElement("Html")]
        public XmlCDataSection HtmlSection
        {
            get { return new XmlDocument().CreateCDataSection(Html); }
            set { Html = value.Value.Replace("\n", "\r\n"); }
        }

        /// <summary>
        /// Gets or sets the RTF to copy.
        /// </summary>
        [XmlIgnore]
        public string Rtf { get; set; }

        /// <summary>
        /// Gets or sets the HTML that should result from the copy.
        /// </summary>
        [XmlIgnore]
        public string Html { get; set; }

        #endregion // Properties

        public static void SaveScenario(Scenario scenario)
        {
            XmlSerializer serializer;
            string path;

            Safety.AssertArgumentNotNull("scenario", scenario);

            serializer = new XmlSerializer(typeof(Scenario));
            path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                "ConvertSourceToHtml Scenarios");
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = System.IO.Path.Combine(path, String.Format(CultureInfo.InvariantCulture,
                    "{0:yyyyMMddHHmmss}.scenario", DateTime.Now));
                using (StreamWriter writer = new StreamWriter(path))
                {
                    serializer.Serialize(writer, scenario);
                    writer.Close();
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Could not save the current scenario.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

}
